<?php

namespace App\Http\Middleware\Request;

use App\Exceptions\ServiceException;
use App\Tools\Signature;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
//跨系统交互签名校验中间件
class AcrossSignatureMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $params = $request->all();
        $url = $request->getUri();
        $log = [
            'params' => $params,
            'url' => $url
        ];
        Log::info('across-接收参数', $log);
        if (!array_key_exists('sign', $params)) {
            throw new ServiceException('sign does not exist !');
        }

        $sign = $params['sign'];
        unset($params['sign']);

        if (!Signature::verify($params, $sign, config('rpc_request.common_rpc_key'))) {
            Log::notice('across-验签失败', $log);

            throw new ServiceException('sign invalid !');
        }

        return $next($request);
    }
}
